﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.IO;
using System.Windows.Markup;

namespace PhoneStore.Presentation.CompositeControl
{
    /// <summary>
    /// Interaction logic for PictureBox.xaml
    /// </summary>
    public partial class PictureBox : UserControl
    {
        public PictureBox()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            if (dialog.ShowDialog().Value)
            {
                ImgSource = dialog.FileName;
            }
        }

        private void SetImage(string uri)
        {
            byte[] buffer = File.ReadAllBytes(uri);
            MemoryStream memoryStream = new MemoryStream(buffer);

            BitmapImage bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.DecodePixelWidth = 320;
            bitmap.DecodePixelHeight = 240;
            bitmap.StreamSource = memoryStream;
            bitmap.EndInit();
            bitmap.Freeze();
            imgPic.Source = bitmap;
        }

        // Dependency Property
        public static readonly DependencyProperty ImgSourceProperty =
             DependencyProperty.Register("ImgSource", typeof(string),
             typeof(PictureBox), new FrameworkPropertyMetadata(null,
                     FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

        // .NET Property wrapper
        public string ImgSource
        {
            get {
                return (string)GetValue(ImgSourceProperty); }
            set {
                SetValue(ImgSourceProperty, value); }
        }


        public new bool IsEnabled
        {
            get
            {
                return btnBrowse.Visibility == Visibility.Visible;
            }
            set
            {
                if (!value)
                    btnBrowse.Visibility = Visibility.Collapsed;
                else
                    btnBrowse.Visibility = Visibility.Visible;
            }
        }
    }

    public abstract class BaseConverter : MarkupExtension
    {
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }
    }

    [ValueConversion(typeof(object), typeof(ImageSource))]
    public class StringToImageSourceConverter : BaseConverter, IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
                          System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                return null;
            }

            string filePath = value.ToString();
            if (File.Exists(filePath))
            {
                byte[] buffer;
                try
                {
                    buffer = File.ReadAllBytes(value.ToString());
                }
                catch
                {
                    return null;
                }
                MemoryStream memoryStream = new MemoryStream(buffer);

                BitmapImage bitmap = new BitmapImage();
                bitmap.BeginInit();
                bitmap.DecodePixelWidth = 320;
                bitmap.DecodePixelHeight = 240;
                bitmap.StreamSource = memoryStream;
                bitmap.EndInit();
                bitmap.Freeze();

                return bitmap;
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter,
                        System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }
}
